import requests
from bs4 import BeautifulSoup
import json

import config


url = "https://t.me/s/" + config.channel_name

response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

channel_title = soup.find('div', {'class': 'tgme_channel_info_header_title'}).text.strip()
channel_description = soup.find('div', {'class': 'tgme_channel_info_description'}).text.strip()

# A function to generate feed JSON data
def generate_feed_json():
    # Create a dictionary for the feed
    feed = {}
    # Add the required version and title keys
    feed['version'] = 'https://jsonfeed.org/version/1.1'
    feed['title'] = channel_title
    # Add the optional home_page_url and feed_url keys
    feed['home_page_url'] = url
    feed['feed_url'] = url + '/feed.json'
    # Add the optional description key
    feed['description'] = channel_description
    # Create a list for the items
    items = []
    # Loop through the posts and create an item for each one
    for post in soup.find_all('div', {'class': 'tgme_widget_message'}):
        # Create a dictionary for the item
        item = {}
        # Add the required id key, using the post url as the unique identifier
        item['id'] = post.find('a', {'class': 'tgme_widget_message_date'})['href']
        # Add the optional url key, using the same value as id
        item['url'] = item['id']
        # Add the optional content_text or content_html key, depending on the post format
        if post.find('div', {'class': 'tgme_widget_message_text'}):
            item['content_text'] = post.find('div', {'class': 'tgme_widget_message_text'}).text.strip()
            item['title'] = post.find('div', {'class': 'tgme_widget_message_text'}).text.strip()
        # Add the optional date_published key, using the datetime attribute of the time element
        item['date_published'] = post.find('time', {'class': 'time'})['datetime']
        # Add the optional image key, using the background-image style of the photo element
        if post.find('a', {'class': 'tgme_widget_message_photo_wrap'}):
            item['image'] = post.find('a', {'class': 'tgme_widget_message_photo_wrap'})['style'].split('(')[1].split(')')[0].replace("\'", "")
        # Add the optional attachments key, using a list of dictionaries for each video element
        if post.find_all('a', {'class': 'tgme_widget_message_video_player'}):
            videos = 0
            for video in post.find_all('a', {'class': 'tgme_widget_message_video_player'}):
                videos += 1
        items.append(item)
    # Add the items list to the feed dictionary
    feed['items'] = items
    # Return the feed dictionary as JSON data
    return json.dumps(feed, indent=4)

# A function to write feed JSON data to a file
def write_feed_json(filename):
    # Open a file with the given filename in write mode
    with open(filename, 'w') as f:
        # Write the feed JSON data to the file
        f.write(generate_feed_json())
        # Close the file
        f.close()

# Call the write_feed_json function with a filename of your choice
write_feed_json('feed.json')

# Print a message to indicate success
print('Feed JSON data written to feed.json')
